home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / Tk / pilbitmap.txt < prev   
Encoding:
Text File  |  2000-06-23  |  4.5 KB  |  150 lines

  1. ====================================================================
  2. The PIL Bitmap Booster Patch
  3. ====================================================================
  4.  
  5. The pilbitmap booster patch greatly improves performance of the
  6. ImageTk.BitmapImage constructor.  Unfortunately, the design of Tk
  7. doesn't allow us to do this from the tkImaging interface module, so
  8. you have to patch the Tk sources.
  9.  
  10. Once installed, the ImageTk module will automatically detect this
  11. patch.
  12.  
  13. (Note: this patch has been tested with Tk 8.0 on Win32 only, but it
  14. should work just fine on other platforms as well).
  15.  
  16. 1. To the beginning of TkGetBitmapData (in generic/tkImgBmap.c), add
  17.    the following stuff:
  18.  
  19. ------------------------------------------------------------------------
  20.     int width, height, numBytes, hotX, hotY;
  21.     char *p, *end, *expandedFileName;
  22.     ParseInfo pi;
  23.     char *data = NULL;
  24.     Tcl_DString buffer;
  25.  
  26. /* ==================================================================== */
  27. /* The pilbitmap booster patch -- patch section                         */
  28. /* ==================================================================== */
  29.  
  30.     char *PILGetBitmapData();
  31.  
  32.     if (string) {
  33.         /* Is this a PIL bitmap reference? */
  34.         data = PILGetBitmapData(string, widthPtr, heightPtr, hotXPtr, hotYPtr);
  35.         if (data)
  36.             return data;
  37.     }
  38.  
  39. /* ==================================================================== */
  40.  
  41.     pi.string = string;
  42.     if (string == NULL) {
  43.         if (Tcl_IsSafe(interp)) {
  44. ------------------------------------------------------------------------
  45.  
  46.  
  47. 2. Append the following to the same file (you may wish to include
  48. Imaging.h instead of copying the struct declaration...)
  49.  
  50. ------------------------------------------------------------------------
  51.  
  52. /* ==================================================================== */
  53. /* The pilbitmap booster patch -- code section                          */
  54. /* ==================================================================== */
  55.  
  56. /* Imaging declaration boldly copied from Imaging.h (!) */
  57.  
  58. typedef struct ImagingInstance *Imaging; /* a.k.a. ImagingImage :-) */
  59.  
  60. typedef unsigned char UINT8;
  61. typedef int INT32;
  62.  
  63. struct ImagingInstance {
  64.  
  65.     /* Format */
  66.     char mode[4+1];     /* Band names ("1", "L", "P", "RGB", "RGBA", "CMYK") */
  67.     int type;           /* Always 0 in this version */
  68.     int depth;          /* Always 8 in this version */
  69.     int bands;          /* Number of bands (1, 3, or 4) */
  70.     int xsize;          /* Image dimension. */
  71.     int ysize;
  72.  
  73.     /* Colour palette (for "P" images only) */
  74.     void* palette;
  75.  
  76.     /* Data pointers */
  77.     UINT8 **image8;     /* Set for 8-bit image (pixelsize=1). */
  78.     INT32 **image32;    /* Set for 32-bit image (pixelsize=4). */
  79.  
  80.     /* Internals */
  81.     char **image;       /* Actual raster data. */
  82.     char *block;        /* Set if data is allocated in a single block. */
  83.  
  84.     int pixelsize;      /* Size of a pixel, in bytes (1 or 4) */
  85.     int linesize;       /* Size of a line, in bytes (xsize * pixelsize) */
  86.  
  87.     /* Virtual methods */
  88.     void (*im_delete)(Imaging *);
  89.  
  90. };
  91.  
  92. /* The pilbitmap booster patch allows you to pass PIL images to the
  93.    Tk bitmap decoder.  Passing images this way is much more efficient
  94.    than using the "tobitmap" method. */
  95.  
  96. char *
  97. PILGetBitmapData(string, widthPtr, heightPtr, hotXPtr, hotYPtr)
  98.     char *string;
  99.     int *widthPtr, *heightPtr;
  100.     int *hotXPtr, *hotYPtr;
  101. {
  102.     char* data;
  103.     char* p;
  104.     int y;
  105.     Imaging im;
  106.  
  107.     if (strncmp(string, "PIL:", 4) != 0)
  108.         return NULL;
  109.  
  110.     im = (Imaging) atol(string + 4);
  111.  
  112.     if (strcmp(im->mode, "1") != 0 && strcmp(im->mode, "L") != 0)
  113.         return NULL;
  114.  
  115.     data = p = (char *) ckalloc((unsigned) ((im->xsize+7)/8) * im->ysize);
  116.  
  117.     for (y = 0; y < im->ysize; y++) {
  118.         char* in = im->image8[y];
  119.         int i, m, b;
  120.         b = 0; m = 1;
  121.         for (i = 0; i < im->xsize; i++) {
  122.             if (in[i] != 0)
  123.                 b |= m;
  124.             m <<= 1;
  125.             if (m == 256){
  126.                 *p++ = b;
  127.                 b = 0; m = 1;
  128.             }
  129.         }
  130.         if (m != 1)
  131.             *p++ = b;
  132.     }
  133.  
  134.     *widthPtr = im->xsize;
  135.     *heightPtr = im->ysize;
  136.     *hotXPtr = -1;
  137.     *hotYPtr = -1;
  138.  
  139.     return data;
  140. }
  141.  
  142. /* ==================================================================== */
  143.  
  144. ------------------------------------------------------------------------
  145.  
  146. 3. Recompile Tk and relink the _tkinter module (where necessary).
  147.  
  148. ====================================================================
  149. Last updated: 97-05-17/fl
  150.